/**
 * @file 数据类基类
 * @exports common.BaseData
 * @author otakustay(otakustay@gmail.com)
 */
define(
    function (require) {
        var u = require('underscore');
        var ubRiaUtil = require('ub-ria/util');
        var RequestStrategy = require('common/RequestStrategy');

        /**
         * @class common.BaseData
         * @extends ub-ria.mvc.RequestManager
         */
        var exports = {};

        exports.constructor = function (entityName) {
            this.$super(arguments);
            this.setRequestStrategy(new RequestStrategy());
        };

        /**
         * 获取列表
         *
         * @return {er.Promise}
         */
        exports.search = function (query) {
            var entityName = this.getEntityName();
            var pascalEntityName = ubRiaUtil.pascalize(entityName);
            return this.request(
                '$entity/list',
                query,
                {
                    method: 'GET',
                    url: '/api/get' + pascalEntityName + 'List'
                }
            );
        };

        /**
         * 获取详情
         *
         * @return {er.Promise}
         */
        exports.findById = function (id) {
            var entityName = this.getEntityName();
            var pascalEntityName = ubRiaUtil.pascalize(entityName);
            id = id ? '/' + id : '';
            return this.request(
                '$entity/detail',
                null,
                {
                    method: 'GET',
                    url: '/api/get' + pascalEntityName + id
                }
            );
        };

        /**
         * 更新
         *
         * @param {entity} entity
         * @return {er.Promise}
         */
        exports.update = function (entity) {
            var entityName = this.getEntityName();
            var pascalEntityName = ubRiaUtil.pascalize(entityName);
            return this.request(
                '$entity/update',
                entity,
                {
                    method: 'POST',
                    url: '/api/add' + pascalEntityName
                }
            );
        };
        /**
         * 保存
         *
         * @param {entity} entity
         * @return {er.Promise}
         */
        exports.save = function (entity) {
            return this.update(entity);
        };

        /**
         * 删除
         *
         * @param {Number} id
         * @return {er.Promise}
         */
        exports.deleteById = function (id) {
            var entityName = this.getEntityName();
            var pascalEntityName = ubRiaUtil.pascalize(entityName);
            return this.request(
                '$entity/delete',
                {
                    id: id
                },
                {
                    method: 'post',
                    url: '/api/delete' + pascalEntityName
                }
            );
        };

        var RequestManager = require('ub-ria/mvc/RequestManager');
        var BaseData = require('eoo').create(RequestManager, exports);

        return BaseData;
    }
);
